﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Dycox.ComponentModel;

namespace Dycox.Utils
{
    public static class Formatting
    {
        public static string ToString(object value)
        {
            return ToString(value, null);
        }

        public static string ToString(object value, PropertyDescriptor property)
        {
            return ToString(value, property, System.Globalization.CultureInfo.CurrentUICulture);
        }

        public static string ToString(object value, PropertyDescriptor property, IFormatProvider formatProvider)
        {

            if (property != null)
            {
                DisplayFormatAttribute dfa = (DisplayFormatAttribute)property.Attributes[typeof(DisplayFormatAttribute)];
                if (dfa != null)
                {
                    return dfa.FormatValue(value, formatProvider);
                }
            }

            if (value != null)
            {
                if (value is DateTime)
                {
                    DateTime dt = (DateTime)value;

                    if (dt == dt.Date)
                        return dt.ToLongDateString();
                    else
                        return dt.ToString("f");
                }
                if (value is bool)
                    return FormatBoolean((bool)value, property, formatProvider);
                else if (value.GetType().IsEnum)
                    return FormatEnum((Enum)value, property, formatProvider);
                return Convert.ToString(value, formatProvider);
            }
            else
                return string.Empty;
        }

        public static string Format(object value, string format)
        {
            return Format(value, format, (PropertyDescriptor)null);
        }

        public static string Format(object value, string format, string nullText)
        {
            return Format(value, format, nullText, null);
        }

        public static string Format(object value, string format, PropertyDescriptor property)
        {
            return Format(value, format, string.Empty, property);
        }

        public static string Format(object value, string format, string nullText, PropertyDescriptor property)
        {
            return Format(value, format, nullText, property, System.Globalization.CultureInfo.CurrentCulture);
        }

        public static string Format(object value, string format, string nullText, PropertyDescriptor property, IFormatProvider formatProvider)
        {
            if (value == null) return nullText;

            if (!string.IsNullOrEmpty(format))
            {
                if (format.IndexOf("{0") < 0)
                    format = "{0:" + format + "}";
                return string.Format(format, value);
            }
            else
                return ToString(value, property, formatProvider);
        }

        private static string FormatBoolean(bool value, PropertyDescriptor property, IFormatProvider formatProvider)
        {
            BooleanTextAttribute bta = null;

            if (property != null)
                bta = (BooleanTextAttribute)property.Attributes[typeof(BooleanTextAttribute)];

            if (bta == null)
                bta = BooleanTextAttribute.Default;

            return value ? bta.TrueText : bta.FalseText;
        }

        private static string FormatEnum(Enum value, PropertyDescriptor property, IFormatProvider formatProvider)
        {
            return EnumHelpers.GetText(value);
        }

        public static string ToString(this DateTime? value, string format)
        {
            if (value == null) return string.Empty;
            else return value.Value.ToString(format);
        }

        public static string ToString(this decimal? value, string format)
        {
            if (value == null) return string.Empty;
            else return value.Value.ToString(format);
        }

    }
}
